package com.cy.dda.web.admin.customer;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.comm.utils.ExportExcelUtils;
import com.cy.dda.model.PageBean;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.customer.CustomerAttachmentAddAO;
import com.cy.dda.model.domain.customer.CustomerAttachment;
import com.cy.dda.model.domain.customer.CustomerCompanyEditRecord;
import com.cy.dda.model.domain.customer.CustomerLiveAddressEditRecord;
import com.cy.dda.model.domain.customer.CustomerMobileEditRecord;
import com.cy.dda.model.query.customer.CustomerListAdminQuery;
import com.cy.dda.model.vo.customer.CustomerDetailInfoAdminVO;
import com.cy.dda.model.vo.customer.CustomerListAdminVO;
import com.cy.dda.service.customer.CustomerService;
import com.cy.dda.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-客户信息管理
 *
 * @author: tanyilin
 * @create: 2018年11月7日 下午4:00:38
 */
@Slf4j
@RestController
@RequestMapping("/admin/customer")
@Api(tags = "后台-客户信息管理")
public class AdminCustomerController extends BaseController {

	@Autowired
	private CustomerService customerService;
	
	@ApiOperation("客户列表")
	@GetMapping("/list")
	public PageBean<CustomerListAdminVO> selectCustomerListAdmin(CustomerListAdminQuery query) {
		PageBean<CustomerListAdminVO> pageBean = customerService.selectCustomerListAdmin(query);
		return pageBean;
	}
	
	@ApiOperation("导出客户列表")
	@GetMapping("/list/export")
	public void listExport(CustomerListAdminQuery query, HttpServletRequest req, HttpServletResponse resp) {
		query.setPage(1);
		query.setLimit(5000);
		List<CustomerListAdminVO> data = this.selectCustomerListAdmin(query).getData();
		String title = "客户列表";
		String[] headers = {"客户", "风险评级", "剩余追偿", "已回款", "催收记录", 
				"委案机构", "委案", "添加时间", "备注"};
		String[] fieldNames = {"name", "riskLevelText", "surplusAmount", "repayAmount", "collectionRecordCount", 
				"orgCount", "casesCount", "createTime", "remark"};
		ExportExcelUtils<CustomerListAdminVO> ex = new ExportExcelUtils<>();
		try(OutputStream out = resp.getOutputStream()) {
			req.setCharacterEncoding("UTF-8");
			resp.setCharacterEncoding("UTF-8");
			resp.setContentType("application/x-download");
			String fileName = CommonUtils.getCurrentTimeAndRandomNumber().concat(".xls");
			fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
			resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			ex.exportExcel(title, headers, fieldNames, data, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation("客户详细信息")
	@GetMapping("/detail/info")
	public Result<CustomerDetailInfoAdminVO> getDetailInfo(@RequestParam(required = true) Long customerId) {
		CustomerDetailInfoAdminVO modelVo = customerService.selectCustomerDetailInfoAdminVO(customerId);
		return ok("", modelVo);
	}
	
	@ApiOperation("客户联系方式修改记录")
	@GetMapping("/get/mobile/edit/record")
	public Result<List<CustomerMobileEditRecord>> selectCustomerMobileEditRecord(@RequestParam(required = true) Long customerId) {
		List<CustomerMobileEditRecord> list = customerService.selectCustomerMobileEditRecord(customerId);
		return ok("", list); 
	}
	
	@ApiOperation("客户居住地址修改记录")
	@GetMapping("/get/live/address/edit/record")
	public Result<List<CustomerLiveAddressEditRecord>> selectCustomerLiveAddressEditRecord(@RequestParam(required = true) Long customerId) {
		List<CustomerLiveAddressEditRecord> list = customerService.selectCustomerLiveAddressEditRecord(customerId);
		return ok("", list); 
	}
	
	@ApiOperation("客户单位信息修改记录")
	@GetMapping("/get/company/edit/record")
	public Result<List<CustomerCompanyEditRecord>> selectCustomerCompanyEditRecord(@RequestParam(required = true) Long customerId) {
		List<CustomerCompanyEditRecord> list = customerService.selectCustomerCompanyEditRecord(customerId);
		return ok("", list); 
	}
	
	@ApiOperation("新增客户附件资料")
	@PostMapping("/do/add/attachment")
	public Result doAddAttachment(@Validated CustomerAttachmentAddAO addAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		CustomerAttachment customerAttachment = new CustomerAttachment();
		BeanUtils.copyProperties(addAO, customerAttachment);
		customerService.addAttachment(customerAttachment);
		return ok();
	}
	
}
